﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Boom.GameObjects
{
    public class Monster : GameObject
    {
        private Random _Rand;
        private int _Level;
        private Vector2 _Target;
        private bool Chase; 

        //Mục tiêu của quái lv2
        public Vector2 Target
        {
            get { return _Target; }
            set { _Target = value; }
        }

        //Lv của quái
        public int Level
        {
            get { return _Level; }
        }

        public Monster(Game Mygame, Vector2 Position, ref Texture2D Texture, int Hp, int Level, int Width, int Height)
            : base(Mygame, Position, ref Texture, Width, Height)
        {
            _Hp = Hp;
            _Power = 1;
            _Status = 2; 
            _Level = Level;
            _HoriFrame = 4;
            _FrameDead = 8;
            _FrameChangeStt = 4;
            _Move = false;
            _Rand = new Random(this.GetHashCode());
        }

        //thay đổi hướng đi ngẫu nhiên khi gặp vật cản
        protected void Action_LV1(GameTime gameTime)
        {
            if (!Move)
            {
                int temp = _VectorA;
                do
                {
                    temp = _Rand.Next(4) + 1;
                }
                while (temp == _VectorA);
                _VectorA = temp;
                Move = true;
            }
            if (_VectorA == 1) MoveLeft(gameTime);
            if (_VectorA == 2) MoveUp(gameTime);
            if (_VectorA == 3) MoveRight(gameTime);
            if (_VectorA == 4) MoveDown(gameTime);
        }

        //đuổi theo mục tiêu bỏ qua vật cản
        protected void Action_LV2(GameTime gameTime)
        {
            Move = true;
            if (Chase == true)
            {
                if (Position.X <= Target.X - 1) MoveRight(gameTime);
                if (Position.X >= Target.X + 1) MoveLeft(gameTime);
                if (Position.X > Target.X - 1 && Position.X < Target.X + 1) Chase = false;
            }
            else
            {
                if (Position.Y <= Target.Y - 1) MoveDown(gameTime);
                if (Position.Y >= Target.Y + 1) MoveUp(gameTime);
                if (Position.Y > Target.Y - 1 && Position.Y < Target.Y + 1) Chase = true;
            }
        }

        protected override void UpdateStatus()
        {
            if (Status == 0) HP = 0;
            if (Status == 1) Immortal = true;
            //if(Status==2)        
            base.UpdateStatus();
        }

        public override void Update(GameTime gameTime)
        {
            _Bounds = new Rectangle((int)Position.X , (int)Position.Y , 40, 40);
            if (Immortal) Move = false;
            if (HP > 0 && !Immortal)
            {
                if (_Level == 1) Action_LV1(gameTime);
                if (_Level == 2) Action_LV2(gameTime);
            }
            base.Update(gameTime);
        }
    }
}
